package cae.components
{

import flash.events.Event;
import flash.events.FocusEvent;
import flash.text.TextField;

import mx.controls.TextInput;
import mx.events.FlexEvent;
import mx.controls.Alert;
	
public class PromptingTextInput extends TextInput
{
	private var _textEmpty:Boolean;
	private var _currentlyFocused:Boolean = false;
	
	public function PromptingTextInput()
	{
		_textEmpty = true;
		
		addEventListener( Event.CHANGE, handleChange );
		addEventListener( FocusEvent.FOCUS_IN, handleFocusIn );
		addEventListener( FocusEvent.FOCUS_OUT, handleFocusOut );
	}
	
	private var _prompt:String = "";
	
	public function get prompt():String
	{
		return _prompt;
	}
	
	[Bindable]
	public function set prompt( value:String ):void
	{
		_prompt = value;
		
		invalidateProperties();
		invalidateDisplayList();
	}
	
	private var _promptFormat:String = '<font color="#666666"><i>[prompt]</i></font>';
	
	public function get promptFormat():String
	{
		return _promptFormat;
	}
	
	public function set promptFormat( value:String ):void
	{
		_promptFormat = value;
		if ( _promptFormat.indexOf( "[prompt]" ) < 0 ) /* crequeo de formato correcto */
		{
			Alert.show("promptFormat invalido")
		}
		invalidateDisplayList();
	}
	
	[Bindable("textChanged")]
    [CollapseWhiteSpace]
    [NonCommittingChangeEvent("change")]
	override public function set text( value:String ):void
	{
		_textEmpty = (!value) || value.length == 0;
		super.text = value;
		invalidateDisplayList();
	}
	
	override public function get text():String
	{
		if ( _textEmpty )
		{
			return "";
		}
		else
		{
			return super.text;
		}
	}
	
	
	private var _displayAsPassword:Boolean = false;
	
	override public function set displayAsPassword(value:Boolean):void {
		_displayAsPassword = value;
		super.displayAsPassword = value;
	}
	override public function get displayAsPassword():Boolean {
		return _displayAsPassword;
	}
	
	override protected function updateDisplayList( unscaledWidth:Number, unscaledHeight:Number ):void
	{
		if ( _textEmpty && _prompt != "" && !_currentlyFocused )
		{
			if ( _promptFormat == "" )
			{
				super.text = _prompt;
			} 
			else
			{
				super.htmlText = _promptFormat.replace( /\[prompt\]/g, _prompt );
			}
			
			if(super.displayAsPassword) {
				var oldVal:Boolean = _displayAsPassword;
				super.displayAsPassword = false;
				_displayAsPassword = oldVal;
			}
		}
		else {
			if(super.displayAsPassword != _displayAsPassword) {
				super.displayAsPassword = _displayAsPassword;
			}
		}
		
		super.updateDisplayList( unscaledWidth, unscaledHeight );
	}
	
	protected function handleChange( event:Event ):void
	{
		_textEmpty = super.text.length == 0;
	}
	
	protected function handleFocusIn( event:FocusEvent ):void
	{
		_currentlyFocused = true;
		
		if ( _textEmpty )
		{
			super.htmlText = "";
			validateNow();
		}
	}
	
	protected function handleFocusOut( event:FocusEvent ):void
	{
		_currentlyFocused = false;
		invalidateDisplayList();
	}
}
}